package com.haois.mp.database.util.jx;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by haol on 2017/8/30.
 * 修改格式化的日期格式
 */
public class DateUtil {

    public static final String yMd = "yyyy-MM-dd";
    public static final String yMd_HZ = "yyyy年MM月dd日";
    public static final String yMdHm = "yyyy-MM-dd HH:mm";
    public static final String yMdHms = "yyyy-MM-dd HH:mm:ss";
    public static final String yMdHmsS = "yyyy-MM-dd HH:mm:ss:SSSS";
    public static final String yMdHm_ = "yyyyMMddHHmm";
    public static final String yMdHmsS_ = "yyyyMMddHHmmssSSS";
    public static final String Md = "MM-dd";


    public static String addHx(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = df1.parse(time);
            return df.format(date);
        } catch (Exception ex) {
            return time;
        }
    }

    public static String getTime(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }

    public static String getTime(String pattern, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String getYMd() {
        return getTime(yMd);
    }

    public static String getYMd(Date time) {
        return getTime(yMd, time);
    }

    public static String getYMdHm() {
        return getTime(yMdHm);
    }

    public static String getYMdHm_() {
        return getTime(yMdHm_);
    }

    public static String getyMdHmsS__() {
        return getTime(yMdHmsS_);
    }

    public static String getYMdHm(Date time) {
        return getTime(yMdHm, time);
    }

    public static String getYMdHms() {
        return getTime(yMdHms);
    }

    public static String getYMdHms(Date time) {
        return getTime(yMdHms, time);
    }

    public static String getYMdHmsS() {
        return getTime(yMdHmsS);
    }

    public static String getYMdHmsS(Date time) {
        return getTime(yMdHmsS, time);
    }

    public static String getTimeStamp() {
        long time = System.currentTimeMillis();
        return String.valueOf(time);
    }

    /**
     * 判断是否润年
     *
     * @param date
     * @return
     */
    public static boolean isLeapYear(Date date) {
        if (date == null)
            return false;

        final SimpleDateFormat sd = new SimpleDateFormat("yyyy");
        String year = sd.format(date);
        int __year = Integer.parseInt(year);
        if ((__year % 4 == 0) && (__year % 100 != 0) || (__year % 400 == 0)) {
            return true;
        } else {
            return false;

        }
    }

    /**
     * 根据指定格式日期转化为字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date, String format) {

        String result = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            result = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取传入日期的上一个月份起始至截止范围 传入 2012-01-01 返回 2011-12-01 00:00:00 和 2011-12-31
     * 23:59:59
     *
     * @param date
     * @return
     */
    public static List<String> obtainPreMonthRange(Date date) {
        if (date == null)
            return null;

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");

        List<String> ranges = new ArrayList<String>(2);
        // 上个月
        final Date preMonth = DateUtils.addMonths(date, -1);
        Calendar c = Calendar.getInstance();
        c.setTime(preMonth);
        int month = c.get(Calendar.MONTH) + 1;
        String __day = "";
        String startTime = "";
        String endTime = "";
        // 润年
        if (month == 2) {
            if (isLeapYear(preMonth)) {
                __day = "29";

            } else {
                __day = "28";

            }
        } else if (month == 1 || month == 3 || month == 5 || month == 7
                || month == 8 || month == 10 || month == 12) {
            __day = "31";
        } else {
            __day = "30";
        }
        startTime = sd.format(preMonth) + "-01 00:00:00";
        endTime = sd.format(preMonth) + "-" + __day + " 23:59:59";
        ranges.add(startTime);
        ranges.add(endTime);

        return ranges;
    }


    /**
     * 字符串日期进行格式转换
     *
     * @param str
     * @param startType 转换前格式
     * @param endType   转换后格式
     * @return
     */
    public static String stringTypeTransfer(String str, String startType, String endType) {
        SimpleDateFormat sf1 = new SimpleDateFormat(startType);
        SimpleDateFormat sf2 = new SimpleDateFormat(endType);
        String sfstr = "";
        try {
            sfstr = sf2.format(sf1.parse(str));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sfstr;
    }

    public static Object formatDate(Object date, String formatType) {
        SimpleDateFormat sd = new SimpleDateFormat(formatType);
        if (date instanceof Date) {
            return sd.format((Date) date);
        } else if (date instanceof String) {
            try {
                return sd.parse((String) date);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }


    /**
     * 格式化日期格式
     *
     * @param argDate
     * @param argFormat
     * @return 格式化后的日期字符串
     */
    public static String formatDate(Date argDate, String argFormat) throws Exception {
        if (argDate == null) {
            return null;
        }
        if (StringUtils.isEmpty(argFormat)) {
            argFormat = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdfFrom = new SimpleDateFormat(argFormat);
        return sdfFrom.format(argDate).toString();
    }

    public static Date stringToDate(String dateString, String format) {
        Date result = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            result = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param date1
     * @param date2
     * @return data2-data1
     */
    public static int daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrentTime() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }

    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是今天
    public static boolean isThisToday(long time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    //判断选择的日期是否是本月
    public static boolean isThisMonth(long time) {
        return isThisTime(time, "yyyy-MM");
    }

    private static boolean isThisTime(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * @param date1
     * @param date2
     * @return data2-data1
     */
    public static int yearsBetween(Date date1, Date date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Integer thisD = Integer.valueOf(sdf.format(date1));
        Integer nowD = Integer.valueOf(sdf.format(date2));
        return nowD - thisD ;
    }
}
